
module EventEditor
  module Commands
    class PlaySe
      def self.show(base, model, edit_command = [], options = {}, &func)
       @dialog = Gtk::Dialog.new("SE",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )

        @model = model
        @dialog.vbox.add(self.create_panel)
        @dialog.vbox.show_all
        
        self.load(edit_command)
        
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = self.command
          func.call(command) 
        end
        @dialog.destroy
      end
      
      def self.create_panel
        vbox = Gtk::VBox.new
        list = EventEditor::IO.get_SEs
        @listbox = ::Gadgets::Listbox.new('Sounds', [String, String], :data =>list)
        
        hbox = Gtk::HBox.new
        b = Gtk::Button.new('Play')
        b.signal_connect('pressed') do
          if @listbox.selected_data != ""
            Audio.play_se("#{$res.se_path}#{@listbox.selected_data}")
          end
        end
        hbox.add(b)
        
        b = Gtk::Button.new('Stop')
        b.signal_connect('pressed') do
          Audio.stop_all_ses
        end
        hbox.add(b)
        
        vbox.pack_start(hbox, false, false)
        vbox.add(@listbox)
        return vbox
      end
      
      def self.load(edit_command)
        unless edit_command.empty?
          @listbox.select(edit_command[1])
        end
      end
      
      def self.command
        return ['play_se', @listbox.selected_data]
      end
      
      def self.command_to_s(model, args, margin = "")
        t = args[1]
        t = "Stop" if t == ""
        s = "<b>SE:</b> #{t}"
        return s
      end
    end
  end
end